package com.jy.adscm.zltx.controller;

import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.QsFileContent;
import com.jy.adscm.zltx.service.QsFileContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/adscm/zltx/QsFileContent")
public class QsFileContentController {
    @Autowired
    QsFileContentService qsFilecontentService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    /**
     * 添加质量体系文本内容
     * @param qsFilecontent
     * @return
     * @throws AdscmSystemException
     */


    /**
     * 修改质量体系内容文件
     * @param qsFilecontent
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/update")
    public SimpleCoreResponse updateQsFileContent(@RequestBody QsFileContent qsFilecontent , HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        qsFilecontent.setFileContentWriter(token.getUser().getId().toString());
        //修改
            if (qsFilecontent.getFileContentId()!=null&&!"".equals(qsFilecontent.getFileContentId())){
                qsFilecontentService.updateQsFileContent(qsFilecontent);
            }else {
                //新增
                qsFilecontent.setIsLocked(-1);
                qsFilecontent.setIsUse(1);
                qsFilecontentService.insert(qsFilecontent);
            }

        return  new SimpleCoreResponse("200","修改成功", null );
    }
    /**
     * 根据文件号查询
     */
//    @RequestMapping("/selectByFileId/{id}")
//     public SimpleCoreResponse selectQsFileContentByFileId(@PathVariable Integer id)throws  AdscmSystemException{
//        return new SimpleCoreResponse("200","查询成功",qsFilecontentService.selectByFileId(id));
//     }
    /**
     * 根据id删除质量体系文件内容
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse deleteById(@PathVariable Long id){

        return new SimpleCoreResponse("200","删除成功", qsFilecontentService.deleteById(id));


    }

    /**
     *
     * @param
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/selectByFileId/{fileId}/{contentType}")
    public SimpleCoreResponse selectQsFileContentByFileId(@PathVariable Integer fileId,@PathVariable Integer contentType)throws  AdscmSystemException{
        return new SimpleCoreResponse("200","查询成功",qsFilecontentService.selectQsFileContentByFileId(fileId,contentType));
    }


}
